package com.jxsz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jxsz.entity.OrderQueryDto;
import com.jxsz.entity.TOrder;
import com.jxsz.service.TOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "工单管理")
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private TOrderService tOrderService;

    @Operation(summary = "保存工单")
    @PostMapping("/save")
    public Map<String,Object> createOrder(@RequestBody TOrder order) {
        tOrderService.saveOrder(order);
        return new HashMap<String,Object>(){
            {
                put("code",200);
                put("msg","success");
            }
        };
    }

    @Operation(summary = "删除工单")
    @PostMapping("/delete")
    public Map<String,Object> deleteOrder(@RequestParam Integer id) {
        tOrderService.removeById(id);
        return new HashMap<String,Object>(){
            {
                put("code",200);
                put("msg","success");
            }
        };
    }

    @Operation(summary = "修改工单")
    @PostMapping("/update")
    public Map<String,Object> updateOrder(@RequestBody TOrder order) {
        tOrderService.saveOrder(order);
        return new HashMap<String,Object>(){
            {
                put("code",200);
                put("msg","success");
            }
        };
    }

    @Operation(summary = "查询工单")
    @PostMapping("/search/{page}/{size}")
    public Map<String,Object> searchOrders(@RequestBody OrderQueryDto orderQueryDto, @PathVariable Integer page, @PathVariable Integer size) {
        Page<TOrder> orderPages = tOrderService.findAllOrders(page, size, orderQueryDto);
        return new HashMap<String,Object>(){
            {
                put("code",200);
                put("msg","success");
                put("data",orderPages);
            }
        };
    }

    @Operation(summary = "分派工单")
    @PostMapping("/fenpai")
    public Map<String, Object> fenpai(@RequestParam Integer id,@RequestParam Integer deptId,@RequestParam String deptName) {
        return tOrderService.fenpai(id, deptId, deptName);
    }


    @Operation(summary = "每个月每个部门的工单总量、超期率。")
    @PostMapping("/queryDeptCount")
    public Map<String, Object> queryDeptCount(@RequestParam Integer deptId,@RequestParam String startTime,@RequestParam String endTime) {
        return tOrderService.queryDeptCount(deptId,startTime,endTime);
    }

    //工单类型(order_type)(必填)0交办 1直接答复 3无效工单
    @Operation(summary = "每个月每个工单类型的工单总量、超期率。")
    @PostMapping("/queryTypeCount")
    public Map<String, Object> queryTypeCount(@RequestParam Integer orderType,@RequestParam String startTime,@RequestParam String endTime) {
        return tOrderService.queryTypeCount(orderType,startTime,endTime);
    }



}
